<?php
$sub_menu = "230150";
include_once("_common.php");

auth_check($auth[$sub_menu], "r");

$colspan = 8;

$g4['title'] = "등급설정";
include_once($g4['admin_path']."/admin.head.php");

$is_coupon = false;
if (file_exists($g4['path']."/plugin/coupon/_config.php"))
    $is_coupon = true;

if (!$is_coupon)
    alert("쿠폰발행기 설치 후 이용하실 수 있습니다.");

include_once("{$g4['path']}/plugin/coupon/_config.php");

$grade = sql_fetch(" select * from {$mw_cash['grade_table']} where gd_id = '{$gd_id}' ");
if (!$grade)
    alert("등급이 존재하지 않습니다.");

$g4['title'] = "{$grade['gd_name']} 등급 쿠폰지급현황";

$t_acc = $t_now = 0;
$grade_old = null;

$sql = "select * from {$mw_cash['grade_table']} order by gd_cash desc, gd_period*-1 desc, gd_point_save desc ";
$qry = sql_query($sql);
for ($i=0; $row=sql_fetch_array($qry); $i++)
{
    if ($row['gd_id'] == $_GET['gd_id']) {
        break;
    }

    if ($row['gd_use'])
        $grade_old = $row;
    else
        $to_cash = '';
}

$sql = "select r.mb_id, r.gd_cash from ( ";
$sql.= "select mb_id, sum(cl_cash) as gd_cash ";
$sql.= "  from {$mw_cash['cash_list_table']} ";
$sql.= " where cl_status = '1' ";
$sql.= "   and (cl_paytype <> '' and cl_paytype <> '서비스' and cl_paytype <> '포인트')";
$sql.= "   and cl_cash > 0 and cl_payment > 0";
if ($grade['gd_period']) {
    $begin = date("Y-m-d 00:00:00", strtotime("-{$grade['gd_period']} day", $g4['server_time']));
    $limit = date("Y-m-d 23:59:59", $g4['server_time']);
    $sql.= " and cl_datetime between '{$begin}' and '{$limit}' ";
}
$sql.= " group by mb_id ";
if ($grade_old['gd_cash'])
    $sql.= " ) as r where r.gd_cash >= '{$grade['gd_cash']}' and r.gd_cash < '{$grade_old['gd_cash']}'  ";
else
    $sql.= " ) as r where r.gd_cash >= '{$grade['gd_cash']}'  ";

$sql.= " order by r.gd_cash desc ";

$qry = sql_query($sql);
$list = array();

for ($i=0; $row2=sql_fetch_array($qry); ++$i)
{
    $mb = get_member($row2['mb_id'], "mb_id, mb_nick, mb_email, mb_homepage");
    $row2['mb_nick'] = get_sideview($mb['mb_id'], $mb['mb_nick'], $mb['mb_email'], $mb['mb_homepage']);

    $sql = "select count(*) as cnt from {$mw_coupon['list_table']} ";
    $sql.= " where cp_rel_table = '{$mw_cash['grade_table']}' ";
    $sql.= "   and cp_rel_id = '{$grade['gd_id']}' ";
    $sql.= "   and mb_id_only = '{$row2['mb_id']}' ";
    $tmp = sql_fetch($sql);

    $row2['coupon_count'] = $tmp['cnt'];

    $list[] = $row2;
}

if ($sst && $sod) {

    $tmp = array();
    foreach ($list as $key => $val) {
        $tmp[$key] = $val[$sst];
    }

    if ($sod == "desc")
        $sort = SORT_DESC;
    else
        $sort = SORT_ASC;

    array_multisort($tmp, $sort, $list);
}

?>

<style>
.stx { background-color:#fff697; }
</style>

<input type="button" class="btn1" value="선택 쿠폰발급" onclick="win_coupon()" style="float:right;">
<a href="mw.grade.php?sfl=mb_id&stx=<?php echo $mb_id?>" style="float:left;"><?php echo subtitle($g4['title'])?></a>

<form name=flist method=post>
<table width=100% cellpadding=0 cellspacing=1>
<colgroup width=50>
<colgroup width=50>
<colgroup width=100>
<colgroup width=''>
<colgroup width=100>
<colgroup width=100>
<colgroup width=100>
<tr><td colspan='<?php echo $colspan?>' class='line1'></td></tr>
<tr class='bgcol1 bold col1 ht center'>
    <td><input type=checkbox name=chkall value='1' onclick='check_all(this.form)'></td>
    <td> 번호 </td>
    <td> 등급 </td>
    <td> 회원ID </td>
    <td> <?=subject_sort_link('gd_cash', "gd_id={$gd_id}")?>충전캐쉬</a> </td>
    <td> <?=subject_sort_link('coupon_count', "gd_id={$gd_id}")?>현재등급<br>발행횟수</a> </td>
    <td> 발행 </td>
</tr>
<tr><td colspan='<?php echo $colspan?>' class='line2'></td></tr>

<?php
for ($i=0, $m=count($list); $i<$m; ++$i) {
    $class = $i%2;

    if ($mb_id && $list[$i]['mb_id'] == $mb_id) {
        $class .= " stx ";
    }
?>
<tr class='list<?php echo $class?> col1 ht center'>
    <td> <input type="checkbox" name="chk[]" class="mb_id" value="<?php echo $list[$i]['mb_id']?>"> </td>
    <td> <?php echo $i+1?> </td>
    <td> <?php echo $grade['gd_name']?> </td>
    <td> <?php echo $list[$i]['mb_nick']?> </td>
    <td> <?php echo number_format($list[$i]['gd_cash'])?> </td>
    <td> <a href="#;" onclick="coupon_log('<?php echo $list[$i]['mb_id']?>')"><?php echo number_format($list[$i]['coupon_count'])?></a> </td>
    <td><input type="button" class="btn1" value="쿠폰" onclick="win_coupon('<?php echo $list[$i]['mb_id']?>')"></td>
</tr>

<?php } ?>

<?php
if (!$i)
    echo "<tr> <td style='height:100px; text-align:center;' colspan='{$colspan}' class='b'> 자료가 없습니다. </td></tr>";
?>

<tr><td colspan='<?php echo $colspan?>' class='line1'></td></tr>
</table>
<input type="button" class="btn1" value="선택 쿠폰발급" onclick="win_coupon()">
</form>

<form name="fcoupon" id="fcoupon" method="post">
<input type="hidden" name="cp_rel_table" value="<?php echo $mw_cash['grade_table']?>">
<input type="hidden" name="cp_rel_id" value="<?php echo $grade['gd_id']?>">
<input type="hidden" name="cp_subject" value="<?php echo "{$grade['gd_name']} 등급 축하 쿠폰"?>">
<input type="hidden" name="toggle_type" value="2">
<input type="hidden" name="cp_mb_id" value="">
</form>

<script>
function win_coupon(mb_id) {
    if (mb_id == undefined) {
        var mb_id = $("input[class=mb_id]:checked").map(function () { return this.value; }).get().join(",");
    }
    if (mb_id == "") {
        alert("회원을 선택해주세요.");
        return false;
    }
    $("input[name=cp_mb_id]").val(mb_id);

    coupon = window.open("about:blank", "coupon", "width=1000,height=600,scrollbars=yes");

    $("#fcoupon").attr("action", "<?php echo $g4['path']?>/plugin/coupon/adm/write.php");
    $("#fcoupon").attr("target", "coupon");
    $("#fcoupon").submit();
}

function coupon_log(mb_id) {
    win_open("mw.grade.coupon.log.php?gd_id=<?php echo $gd_id?>&mb_id="+mb_id, "coupon_log", "width=600,height=400,scrollbars=yes");
}
</script>

<?php

include_once ($g4['admin_path']."/admin.tail.php");
